<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Model_User extends Zend_Db_Table{
	
	protected $_name    = "users";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            if(isset($data['id']) && $this->checkExists($data['id'])){
                $id = $data['id'];
                
                $db->update("users", $data, "id = $id");
            }else{
                $profile = array();
                
                $db->insert('users', $data);
                $user_id = $db->lastInsertId("users");
                
                $db->insert("profiles", $profile);
                $profile_id = $db->lastInsertId("profiles");
                
                $user_profiles['user_id'] = $user_id;
                $user_profiles['profile_id'] = $profile_id;
                
                $db->insert("user_profiles", $user_profiles);
            }
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getDatas($options = null, $cols = array(), $join = false){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        if(count($cols) == 0){
            $cols = array('id', 'name', 'username', 'email', 'created', 'status');
        }
        
        $select = $db->select()
                    ->from(array('u' => 'users'), $cols);  
              
        if($join){
            $select->join(array('ug' => 'user_groups'), "ug.id = u.user_group", array('group_name'))
                    ->join(array('up' => 'user_profiles'), "up.user_id = u.id", array('profile_id AS profile'));
        }
                 
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        if(($options != null && !array_key_exists('order', $options)) || $options == null){
            $select->order('u.name');
        }
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($id){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $select = $db->select()
                        ->from(array('u' => 'users'), array('id', 'name', 'username', 'user_group', 'email', 'status'))
                        ->where('u.id = ?', $id, 'INTEGER');
            
            $rs = $db->fetchRow($select);
            
            return $rs;
        }
        
        return false;
    }
    
    public function deleteData($id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->delete("users", "id = $id");
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function checkExists($id){
        $option = array('table' => 'users', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
}